<?php

/***  DOCUMENTATION LAYER

CEO Element Factory Class

Name: CeoFormElementFactory
Last Update: Jul 2007
Author: Tom at klenwell@gmail.com

DESCRIPTION
  This class gets called by the CeoForm object and based on the type passed
  to it (assuming it's a valid one), will create an object of the specified
  element object sub-class (e.g. CeoFormElementInputText, parent class: 
  CeoFormElement) and return a *reference* to the Form object EL array (i.e.
  $CeoForm->EL[$el_name] where it is accessible to the global script scope.

METHODS
  MAGIC
  CeoFormElementFactory($debug=0, $oid=null)    *php 4 constructor*
  __construct($debug, $oid)          *php 5 constructor*
  __destruct()  
  
  PUBLIC
  print_d($message, $color='c33')
  print_r()
  dump()
  &make()
  
  PRIVATE
  _set_filename()
  _set_dirpath()
  
USAGE
  should be used internally by the CeoForm class

NOTES
  For more info on the factory pattern, see http://www.php.net/manual/en/language.oop5.php

______________________________________________________________________________*/

// Load Base Element Class
require_once('element.class.php');


// CeoFormElementFactory
/*____________________________________________________________________________*/
class CeoFormElementFactory
{
/* PUBLIC PROPERTIES */
var $debug = 0;
var $class_name = __CLASS__;
var $oid = '';
var $DS = DIRECTORY_SEPARATOR;

// control arrays
var $VALID = array();

// basic arguments
var $type = '';
var $name = '';
var $label = '';
var $is_required = 0;

/* PRIVATE PROPERTIES */
var $_arg5 = null;
var $_arg6 = null;
var $_arg7 = null;
var $_arg8 = null;

// child class names
var $_input_text_class_fname = 'input_text.class.php';
var $_textarea_class_fname = 'textarea.class.php';
var $_is_invalid = 0;

var $_filename = '';
var $_dirpath = '';


/* ** MAGIC METHODS ** */
// php4 constructor
function CeoFormElementFactory($name, $type, $label, $is_required=0, $arg5=null, $arg6=null, $arg7=null, $arg8=null, $debug=0)
{
  $this->__construct($name, $type, $label, $is_required, $arg5, $arg6, $arg7, $arg8, $debug);
  register_shutdown_function( array($this, '__destruct') );
}
// END constructor

// php5 constructor
function __construct($name, $type, $label, $is_required, $arg5, $arg6, $arg7, $arg8, $debug)
{
  // default
  $this->oid = $type . '_factory';
  $this->_set_filename();
  $this->_set_dirpath();
  $this->debug = $debug;
  
  // validation arrays
  $this->VALID = CeoFormElement::get_valid_types();
  
  // assign args
  $this->type = $type;
  $this->name = $name;
  $this->label = $label;
  $this->is_required = ( empty($is_required) ) ? 0 : $is_required;
  
  // variable args
  if ( !empty($arg5) ) $this->_arg5 = $arg5;
  if ( !empty($arg6) ) $this->_arg6 = $arg6;
  if ( !empty($arg7) ) $this->_arg7 = $arg7;
  if ( !empty($arg8) ) $this->_arg8 = $arg8;
  
  // general validation
  if ( !in_array($this->type, $this->VALID['TYPE']) )
  {
    $this->_is_invalid = 1;
    trigger_error("invalid element type [$this->type]", E_USER_WARNING);
    return 0;
  }
}
// END constructor

// destructor
function __destruct()
{
  if ( $this->debug ) $this->print_d("destroying class {$this->class_name} (oid: {$this->oid})");
}
// END destructor



/* ** PUBLIC METHODS ** */
// method: make
function &make()
{
  // debug
  if ( $this->debug ) $this->print_d('debugging is active for oid ' . $this->oid . ' (class: ' .__CLASS__. ')');
  
  // abort invalid types (we've already thrown the error)
  if ( $this->_is_invalid ) return;

  // child class constructor tree
  if ( in_array($this->type, $this->VALID['INPUT_TEXT']) )
  {
    return $this->add_input_text();
  }
  elseif ( in_array($this->type, $this->VALID['TEXTAREA']) )
  {
    return $this->add_textarea();
  }
  else
  {
    trigger_error("case trap: type $type not recognized (validity check failed)");
    return 0;
  }
}

// method: add input text field
function &add_input_text()
{
  if ( $this->debug ) $this->print_d("calling CeoFormElementInputText for element [{$this->name}]");
  require_once($this->_input_text_class_fname);
  $maxlength = $this->_arg5; 
  $minlength = ( !$this->is_required || empty($this->_arg6) ) ? 0 : $this->_arg6;
  $debug = $this->debug;
  $this->Element = new CeoFormElementInputText($this->name, $this->type, $this->label, $this->is_required, $maxlength, $minlength, $debug);
  return $this->Element;
}

// method: add input text field
function &add_textarea()
{
  if ( $this->debug ) $this->print_d("calling CeoFormElementTextarea for element [{$this->name}]");
  require_once($this->_textarea_class_fname);
  $maxwords = $this->_arg5; 
  $minwords = ( !$this->is_required || empty($this->_arg6) ) ? 0 : $this->_arg6;
  $WHITE_TAG = $this->_arg7;
  $WHITE_ATT = $this->_arg8;
  $debug = $this->debug;
  $this->Element = new CeoFormElementTextarea($this->name, $this->type, $this->label, $this->is_required, $maxwords, $minwords, $WHITE_TAG, $WHITE_ATT, $debug);
  return $this->Element;
}

// method: print_d
function print_d($message, $color='#c33')
{
  $out = "<div style='line-height:1.5em; font-family:monospace; color:$color;'>$message</div>";
  echo $out;
  return;
}
// END method

// method: print_r
function print_r($Mixed)
{
  $return = htmlspecialchars(print_r($Mixed, 1));
  $return = "<pre>$return</pre>";
  return $return;
}
// END method

// method: dump
function dump()
{
  echo $this->print_r($this);
  return;
}
// END method



/* ** PRIVATE METHODS ** */
function _set_filename() { $this->_filename = basename(__FILE__); }
function _set_dirpath() { $this->_dirpath = dirname(__FILE__) . $this->DS; }

} // end class
/*____________________________________________________________________________*/

?>
